package manage.student;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;

public class LoginView extends JFrame {
    JLabel nameLabel=new JLabel("学生管理系统",JLabel.CENTER);

    //将JPanel设置为弹簧布局
    SpringLayout springLayout=new SpringLayout();
    JPanel centerJpanel=new JPanel(springLayout);
    JLabel useridLabel=new JLabel("用户名:");
    JTextField userText=new JTextField();
    JLabel passwordLabel=new JLabel("密码:");
    JTextField pwdText=new JTextField();
    JButton loginBtn=new JButton("登录");
    JButton resetBtn=new JButton("重置");
    SystemTray systemTray;
    TrayIcon trayIcon;
    LoginHandler loginHandler;




    public LoginView(){
        super("学生管理系统");
        loginHandler=new LoginHandler(this);
        Container contentPane = getContentPane();

        nameLabel.setFont(new Font("宋体",Font.PLAIN,40));
        nameLabel.setPreferredSize(new Dimension(80,80));
        Font font = new Font("楷体", Font.PLAIN, 20);
        useridLabel.setFont(font);
        passwordLabel.setFont(font);
        loginBtn.setFont(font);
        resetBtn.setFont(font);


        //布局
        Spring titleWidth=Spring.width(useridLabel);
        Spring titleTextWidth=Spring.width(userText);
        Spring space=Spring.constant(20);
        Spring width=Spring.sum(Spring.sum(titleWidth,titleTextWidth),space);
        int offset=width.getValue()/2;
        //水平居中处理
        SpringLayout.Constraints userid=springLayout.getConstraints(useridLabel);
        springLayout.putConstraint(SpringLayout.EAST,useridLabel,-offset,SpringLayout.HORIZONTAL_CENTER,centerJpanel);
        springLayout.putConstraint(SpringLayout.NORTH,useridLabel,20,SpringLayout.NORTH,centerJpanel);
        //弹簧布局
        springLayout.putConstraint(SpringLayout.WEST,userText,20,SpringLayout.EAST,useridLabel);
        springLayout.putConstraint(SpringLayout.NORTH,userText,0,SpringLayout.NORTH,useridLabel);
        //password组件布局
        springLayout.putConstraint(SpringLayout.EAST,passwordLabel,0,SpringLayout.EAST,useridLabel);
        springLayout.putConstraint(SpringLayout.NORTH,passwordLabel,20,SpringLayout.SOUTH,useridLabel);
        springLayout.putConstraint(SpringLayout.WEST,pwdText,20,SpringLayout.EAST,passwordLabel);
        springLayout.putConstraint(SpringLayout.NORTH,pwdText,0,SpringLayout.NORTH,passwordLabel);
        //login组件
        springLayout.putConstraint(SpringLayout.EAST,loginBtn,50,SpringLayout.EAST,passwordLabel);
        springLayout.putConstraint(SpringLayout.NORTH,loginBtn,20,SpringLayout.SOUTH,passwordLabel);
        //reset组件
        springLayout.putConstraint(SpringLayout.WEST,resetBtn,20,SpringLayout.EAST,loginBtn);
        springLayout.putConstraint(SpringLayout.NORTH,resetBtn,0,SpringLayout.NORTH,loginBtn);



        //将组件加入到Jpanel中
        centerJpanel.add(useridLabel);
        userText.setPreferredSize(new Dimension(200,30));
         centerJpanel.add(userText);
         centerJpanel.add(passwordLabel);
        pwdText.setPreferredSize(new Dimension(200,30));
        centerJpanel.add(pwdText);
        loginBtn.addActionListener(loginHandler);
        centerJpanel.add(loginBtn);
        resetBtn.addActionListener(loginHandler);
        centerJpanel.add(resetBtn);

        contentPane.add(nameLabel,BorderLayout.NORTH);
        contentPane.add(centerJpanel,BorderLayout.CENTER);
        //设置默认按钮
         getRootPane().setDefaultButton(loginBtn);

        //事件处理
        if(SystemTray.isSupported()){
            systemTray=SystemTray.getSystemTray();

        }




        //JFrame设置
        setSize(600,400);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(true);
        setLocationRelativeTo(null);//将jf置中
    }

    public static void main(String[] args) {
        new LoginView();
    }

    public JTextField getUserText() {
        return userText;
    }

    public JTextField getPwdText() {
        return pwdText;
    }
}
